package com.ocm.core.domain;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.UniqueConstraint;

@Entity
@Table(name = "STORAGE", uniqueConstraints = @UniqueConstraint(columnNames = "STORAGE_NAME"))
@NamedQueries( {
      @NamedQuery(name = Storage.FIND_ALL, query = "SELECT storage FROM Storage storage"),
      @NamedQuery(name = Storage.FIND_NAME, query = "SELECT storage FROM Storage storage WHERE LOWER(storage.name)=LOWER(:name)") })
public class Storage implements Persistable {

   public static final String FIND_ALL = "Storage.FIND_ALL";

   public static final String FIND_NAME = "Storage.FIND_NAME";

   public static final String NAME_PARA = "name";

   @Id
   @GeneratedValue(strategy = GenerationType.TABLE, generator = "GEN_STORAGE")
   @TableGenerator(name = "GEN_STORAGE", table = "SEQUENCE_LIST", allocationSize = 1, initialValue = 1, pkColumnName = "NAME", valueColumnName = "NEXT_SEQUENCE", pkColumnValue = "STORAGE")
   @Column(name = "STORAGE_ID")
   private Long id;

   @Column(name = "STORAGE_NAME", unique = true, nullable = false)
   private String name;

   @Column(name = "DISPLAY_NAME", nullable = false)
   private String displayName;

   @Column(name = "STORAGE_URL", nullable = false)
   private String storageURL;

   @Column(name = "STORAGE_UID", nullable = false)
   private String storageUID;

   @Column(name = "REAL_PATH", nullable = false)
   private String realPath;

   @Column(name = "DESCRIPTION")
   private String description;

   @OneToMany(mappedBy = "storage", cascade = CascadeType.ALL)
   private Set<ClassDefinition> classDefs;

   @OneToMany(mappedBy = "storage", cascade = CascadeType.ALL)
   private Set<PropertyDefinition> propertyDefs;

   @Column(name = "CREATED_USER")
   private String createdUser;

   @Column(name = "CREATED_DATE")
   private Date createdDated;

   @Column(name = "MODIFYED_USER")
   private String modifyedUser;

   @Column(name = "MODIFYED_DATE")
   private Date modifyedDated;

   public Storage() {
      classDefs = new HashSet<ClassDefinition>();
      propertyDefs = new HashSet<PropertyDefinition>();
   }

   public Long getId() {
      return id;
   }

   public void setId(Long id) {
      this.id = id;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public String getDisplayName() {
      return displayName;
   }

   public void setDisplayName(String displayName) {
      this.displayName = displayName;
   }

   public String getStorageURL() {
      return storageURL;
   }

   public void setStorageURL(String storageURL) {
      this.storageURL = storageURL;
   }

   public String getDescription() {
      return description;
   }

   public void setDescription(String description) {
      this.description = description;
   }

   public List<ClassDefinition> getClassDefs() {
      return new ArrayList<ClassDefinition>(classDefs);
   }

   public void setClassDefs(List<ClassDefinition> classDefs) {
      this.classDefs.clear();
      this.classDefs.addAll(classDefs);
   }

   public List<PropertyDefinition> getPropertyDefs() {
      return new ArrayList<PropertyDefinition>(propertyDefs);
   }

   public void setPropertyDefs(List<PropertyDefinition> propertyDefs) {
      this.propertyDefs.clear();
      this.propertyDefs.addAll(propertyDefs);
   }

   public String getStorageUID() {
      return storageUID;
   }

   public void setStorageUID(String storageUID) {
      this.storageUID = storageUID;
   }

   public String getRealPath() {
      return realPath;
   }

   public void setRealPath(String realPath) {
      this.realPath = realPath;
   }

   public String getCreatedUser() {
      return createdUser;
   }

   public void setCreatedUser(String createdUser) {
      this.createdUser = createdUser;
   }

   public Date getCreatedDated() {
      return createdDated;
   }

   public void setCreatedDated(Date createdDated) {
      this.createdDated = createdDated;
   }

   public String getModifyedUser() {
      return modifyedUser;
   }

   public void setModifyedUser(String modifyedUser) {
      this.modifyedUser = modifyedUser;
   }

   public Date getModifyedDated() {
      return modifyedDated;
   }

   public void setModifyedDated(Date modifyedDated) {
      this.modifyedDated = modifyedDated;
   }

   @Override
   public boolean equals(Object obj) {
      Storage propertyCompare = (Storage) obj;
      if (this.getId().longValue() == propertyCompare.getId().longValue()) {
         return true;
      }
      return false;
   }
}
